<?php
/**
 * Copyright (c) 2009 Nicholas Aiello  <nikko.aiello@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
**/

define('SITE_ROOT', dirname(__FILE__) . '/../../');

require SITE_ROOT . 'includes/config.php';
require SITE_ROOT . 'lib/classes/optimizer.class.php';

class JSpeed {
	
	// array of scripts to optimize
	public $pre;
	// array of optimized scripts
	public $post;
	
	// optimize engine
	private $_engine;
	// show modified script
	private $_output;
	// minify code
	private $_minify;
	// number of scripts
	private $_count;

	//
	public function __construct($script=array(), $minify=true) {
		$this->_engine = new Optimizer();
		$this->pre = is_array($script) ? $script : array($script);
		$this->post = array();
		$this->_output = false;
		$this->_minify = $minify;
		$this->_count = 0;
	}
	
	public function &getScripts() {
		return $this->pre;
	}
	
	public function &getOptimizedScripts() {
		return $this->_count == 1 ? $this->post[0] : $this->post;
	}
	
	// add a script to the scripts array
	public function addScript(&$script) {
		++$this->_count;
		$this->pre[] = $script;
	}
	
	public function setMinify($minify=true) {
		$this->minify = is_bool($minify) ? $minify : $this->minify;
	}
	
	public function setOutput($output=false) {
		$this->_output = $output;
	}
	
	// iterate through the scripts, and substitute the optimization patterns
	public function optimize() {
		$this->_engine->execute($this->pre, $this->post, $this->_minify);
	}
	
	public function benchmark($element_id=null) {
		ob_start();
		echo '<script type="text/javascript">' , "\n";
		
		echo '	var startTime = new Date();' , "\n";
		echo '// PRE SCRIPTS' , "\n";
		for ($i = 0, $len = count($this->pre); $i < $len; ++$i) {
			echo $this->pre[$i] , "\n";
		}
		echo '	var endTime = ((new Date()).getTime() - startTime.getTime()) / 1000;' , "\n";
		if ($element_id) {
			echo '	document.getElementById("' , $element_id , '").innerHTML = endTime;' , "\n\n"; 
		}
		else {
			echo "	if (typeof console != 'undefined') {\n console.log(endTime);\n }\n else {\n alert(endTime);\n }\n\n";
		}
		
		echo '	var startTime = new Date();' , "\n";
		echo '// POST SCRIPTS' , "\n";
		for ($i = 0, $len = count($this->post); $i < $len; ++$i) {
			echo $this->post[$i] , "\n";
		}
		echo '	var endTime = ((new Date()).getTime() - startTime.getTime()) / 1000;' , "\n";
		if ($element_id) {
			echo '	document.getElementById("' , $element_id , '").innerHTML = endTime;' , "\n"; 
		}
		else {
			echo "	if (typeof console != 'undefined') {\n console.log(endTime);\n }\n else {\n alert(endTime);\n }\n";
		}
		
		echo '</script>' , "\n";
		
		ob_end_flush();
	}
}

?>